home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / FixPath.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  5KB  |  231 lines

  1. /*
  2. **    FixPath.c
  3. **
  4. **    Fix the current Process search patch list by faking a CLI
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* This is how a linked list of directory search paths looks like. */
  15.  
  16. struct Path
  17. {
  18.     BPTR path_Next;    // Pointer to next entry
  19.     BPTR path_Lock;    // The drawer in question
  20. };
  21.  
  22.     /* CloneBString(BSTR Contents,WORD Len):
  23.      *
  24.      *    Reserve space for a BString, optionally copy an
  25.      *    existing BString into the new string.
  26.      */
  27.  
  28. STATIC BSTR __regargs
  29. CloneBString(BSTR Contents,WORD Len)
  30. {
  31.     UBYTE    *Source,
  32.             *String;
  33.  
  34.     if(Contents)
  35.     {
  36.         Source = BADDR(Contents);
  37.  
  38.         if(*Source > Len)
  39.             Len = *Source;
  40.     }
  41.     else
  42.         Source = NULL;
  43.  
  44.     if(String = AllocVec(Len + 1,MEMF_ANY))
  45.     {
  46.         if(Source)
  47.             CopyMem(Source,String,((WORD)*String) + 1);
  48.         else
  49.             *String = 0;
  50.     }
  51.  
  52.     return(MKBADDR(String));
  53. }
  54.  
  55.     /* FreeBString(BSTR String):
  56.      *
  57.      *    Free the memory allocated for a BString.
  58.      */
  59.  
  60. STATIC VOID __regargs
  61. FreeBString(BSTR String)
  62. {
  63.     APTR Mem = BADDR(String);
  64.  
  65.     if(String)
  66.         FreeVec(Mem);
  67. }
  68.  
  69.     /* DeletePath(BPTR StartPath):
  70.      *
  71.      *    Delete a path list as created by CreatePath().
  72.      */
  73.  
  74. STATIC VOID __regargs
  75. DeletePath(BPTR StartPath)
  76. {
  77.     struct Path    *First,
  78.                 *Next;
  79.  
  80.     for(First = BADDR(StartPath) ; First ; First = Next)
  81.     {
  82.         Next = BADDR(First -> path_Next);
  83.  
  84.         UnLock(First -> path_Lock);
  85.  
  86.         FreeVec(First);
  87.     }
  88. }
  89.  
  90.     /* ClonePath(BPTR StartPath):
  91.      *
  92.      *    Clone a given path list.
  93.      */
  94.  
  95. STATIC BPTR __regargs
  96. ClonePath(BPTR StartPath)
  97. {
  98.     struct Path    *First    = NULL,
  99.                 *Last    = NULL,
  100.                 *List,
  101.                 *New;
  102.  
  103.     for(List = BADDR(StartPath) ; List ; List = BADDR(List -> path_Next))
  104.     {
  105.         if(New = AllocVec(sizeof(struct Path),MEMF_ANY))
  106.         {
  107.             if(!(New -> path_Lock = DupLock(List -> path_Lock)))
  108.             {
  109.                 FreeVec(New);
  110.  
  111.                 DeletePath(MKBADDR(First));
  112.  
  113.                 return(NULL);
  114.             }
  115.             else
  116.                 New -> path_Next = NULL;
  117.         }
  118.         else
  119.         {
  120.             DeletePath(MKBADDR(First));
  121.  
  122.             return(NULL);
  123.         }
  124.  
  125.         if(Last)
  126.             Last -> path_Next = MKBADDR(New);
  127.  
  128.         if(!First)
  129.             First = New;
  130.  
  131.         Last = New;
  132.     }
  133.  
  134.     return(MKBADDR(First));
  135. }
  136.  
  137.     /* DeleteCLI(struct CommandLineInterface *CLI):
  138.      *
  139.      *    Delete a CLI structure as created by CloneCLI.
  140.      */
  141.  
  142. VOID __regargs
  143. DeleteCLI(struct CommandLineInterface *CLI)
  144. {
  145.     if(CLI)
  146.     {
  147.         DeletePath(CLI -> cli_CommandDir);
  148.  
  149.         Close(CLI -> cli_CurrentOutput);
  150.         Close(CLI -> cli_CurrentInput);
  151.  
  152.         FreeBString(CLI -> cli_CommandFile);
  153.         FreeBString(CLI -> cli_Prompt);
  154.         FreeBString(CLI -> cli_CommandName);
  155.         FreeBString(CLI -> cli_SetName);
  156.  
  157.         FreeVec(CLI);
  158.     }
  159. }
  160.  
  161.     /* CloneCLI(struct Message *Message):
  162.      *
  163.      *    Clone a CLI structure, as supplied by another process.
  164.      */
  165.  
  166. struct CommandLineInterface * __regargs
  167. CloneCLI(struct Message *Message)
  168. {
  169.     struct Process *Source;
  170.  
  171.     if(!Message -> mn_ReplyPort || (Message -> mn_ReplyPort -> mp_Flags & PF_ACTION) != PA_SIGNAL || !TypeOfMem(Message -> mn_ReplyPort -> mp_SigTask))
  172.         return(NULL);
  173.     else
  174.         Source = Message -> mn_ReplyPort -> mp_SigTask;
  175.  
  176.     if(Source -> pr_Task . tc_Node . ln_Type == NT_PROCESS && Source -> pr_CLI)
  177.     {
  178.         struct CommandLineInterface    *SourceCLI = BADDR(Source -> pr_CLI),
  179.                                     *DestCLI;
  180.  
  181.         if(DestCLI = AllocVec(sizeof(struct CommandLineInterface),MEMF_ANY))
  182.         {
  183.             CopyMem(SourceCLI,DestCLI,sizeof(struct CommandLineInterface));
  184.  
  185.             if(DestCLI -> cli_SetName = CloneBString(SourceCLI -> cli_SetName,255))
  186.             {
  187.                 if(DestCLI -> cli_CommandName = CloneBString(SourceCLI -> cli_CommandName,255))
  188.                 {
  189.                     if(DestCLI -> cli_Prompt = CloneBString(SourceCLI -> cli_Prompt,255))
  190.                     {
  191.                         if(DestCLI -> cli_CommandFile = CloneBString(NULL,255))
  192.                         {
  193.                             DestCLI -> cli_Result2        = 0;
  194.                             DestCLI -> cli_ReturnCode    = 0;
  195.                             DestCLI -> cli_Interactive    = DOSFALSE;
  196.                             DestCLI -> cli_Background    = DOSFALSE;
  197.                             DestCLI -> cli_Module        = NULL;
  198.  
  199.                             if(DestCLI -> cli_CurrentInput = Open("NIL:",MODE_NEWFILE))
  200.                             {
  201.                                 if(DestCLI -> cli_CurrentOutput = Open("NIL:",MODE_NEWFILE))
  202.                                 {
  203.                                     DestCLI -> cli_StandardInput    = DestCLI -> cli_CurrentInput;
  204.                                     DestCLI -> cli_StandardOutput    = DestCLI -> cli_CurrentOutput;
  205.                                     DestCLI -> cli_CommandDir        = ClonePath(DestCLI -> cli_CommandDir);
  206.  
  207.                                     return(DestCLI);
  208.                                 }
  209.  
  210.                                 Close(DestCLI -> cli_CurrentInput);
  211.                             }
  212.  
  213.                             FreeBString(DestCLI -> cli_CommandFile);
  214.                         }
  215.  
  216.                         FreeBString(DestCLI -> cli_Prompt);
  217.                     }
  218.  
  219.                     FreeBString(DestCLI -> cli_CommandName);
  220.                 }
  221.  
  222.                 FreeBString(DestCLI -> cli_SetName);
  223.             }
  224.  
  225.             FreeVec(DestCLI);
  226.         }
  227.     }
  228.  
  229.     return(NULL);
  230. }
  231.